<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>我的购物车-同乡烩</title>
  <link href="css/bootstrap.min.css" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="./css/style.css">
  <script src="js/jquery-3.4.1.js" type="application/javascript"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/js.cookie.js" type="application/javascript"></script>
  <script src="js/jquery.cookie.js"></script>
  <script type="text/javascript">
    var  current_data_counts;
    var orderPrice;
    $(function () {
     /* CheckLogin();*/
      postrequest();
      /*下一页*/
      $("#nextPage").click(function () {
        var val = $("#txt").val();
        val++;
        $("#txt").val(val);
        postrequest();
      });
      /*上一页*/
      $("#previousPage").click(function () {
        var val = $("#txt").val();
        val--;
        $("#txt").val(val);
        postrequest();
      });
      //全选
      $("#selectAll").click(function () {
        var ck = $(this).prop("checked");
        $("input[name=select]").prop("checked", ck);
        totalPrice();
      });
      //
      $("#show_cart").on("click","#Oneselect",function () {
        totalPrice();
      });

    });
    //post请求数据
    function postrequest() {
      var val = $("#txt").val();
      $.post("cart/findShop.do", {pageNum: val}, function (data) {
        //不出现重复数据
        $("#show_cart").empty();
        var str = "";
        //计算商品数量
        current_data_counts = data.length;
        $("#totleN").text(current_data_counts);
        for (i in data) {
          str += "<div class='content2 center'>"
                  + "<div class='sub_content fl'>"
                  + "<input type='checkbox' value='quanxuan' class='quanxuan' name='select' id='Oneselect'/>"
                  + "</div>"
                  + "<div class='sub_content fl'><img src='images/"+data[i].image+"' alt='暂无图片'/></div>"
                  + "<div class='sub_content fl ft20'>" + data[i].name + "</div>"
                  + "<div class='sub_content fl '>" + data[i].price + "</div>"
                  + "<div class='sub_content fl'>"
                  + "<input class='shuliang' type='number' value='1' step='1' min='1' onchange='setp(this)'>"
                  + "</div>"
                  + "<div class='sub_content fl'>"+data[i].price+"</div>"
                  + "<div class='sub_content fl'><button value='a' onclick='del(" + data[i].foodid + ")'>x</button></div>"
                  + "<div class='cleas' hidden>" + data[i].foodid + "</div>"
                  + "</div>";
        }
        $("#show_cart").append(str);
      }, "json")
    }
    //验证登录
    function CheckLogin() {
      $.ajax({
        type:"post",
        dataType:"json",
        url:"user/getLogin.do",
        success:function (data) {
          if(data.result!=0){
            $("#current_user").empty();
            var str= "<ul>"
                    +"<li><a id='current_username' href='self_info' target='_blank'>"+data.result+"</a></li>"
                    +"<li>|</li>"
                    +"<li><a href='order_center'>订单通知</a></li>"
                    +"</ul>"
            $("#current_user").append(str);
          }else{
            var url="login";
            Cookies.remove("login_name");
            Cookies.remove("login_pwd");
            location.href="login.html";
          }
        }
      })
    }
    //根据商品数量来定价格
    function setp(obj) {
      var price = $(obj).parent().prev().text();
      var num=$(obj).val();
      var tprice=price*num;
      $(obj).parent().next().text(tprice+"元");
    }
    //计算总的价格
    function totalPrice() {
      var t = 0;
      var sn=0;
      $("input[name=select]").each(function() {
        if($(this).prop("checked")) {
          //找到小计列中的值进行累加
          var p = $(this).parent().next().next().next().next().next().text();
          t += parseInt(p);
          sn+=1;
        }
        if(sn==current_data_counts){
          $("#selectAll").prop("checked",1);
        }else{
          $("#selectAll").prop("checked",0);
        }
        $("#selectN").text(sn);
        $("#totalP").text(t+"元");
        orderPrice=t;
      })
    }

    //结算
    function SettleAccount(){
      /*var ordername = $.cookie('ordername');*/
      var address = $.cookie('address');
      $.ajax({
        type:"get",
        url:"order/addOrder.do",
        data:{
          "cid":1,
          "address":address,
          "paymentstatus":1,
          "price":orderPrice
        },
        dataType: "json",
        success:function (data){
          if(data.result==1){
            location.href="user-indent.html"
          }
        }
      })

    }
  </script>
</head>
<body>
<!-- start header -->
<!--end header -->

<!-- start banner_x -->
<div class="banner_x center">
  <a href="index" target="_blank">
    <div class="logo fl"></div>
  </a>

  <div class="wdgwc fl ml40">我的购物车</div>
  <div class="wxts fl ml20">温馨提示：产品是否购买成功，以最终下单为准哦，请尽快结算</div>
  <div class="dlzc fr" id="current_user">
    <ul>
      <li><a href="orderfit.html" target="_blank">添加地址信息</a></li>
    </ul>

  </div>
  <div class="clear"></div>
</div>
<div class="xiantiao"></div>
<div class="gwcxqbj">
  <div class="gwcxd center">
    <div class="top2 center">
      <div class="sub_top fl" id="check1">
        <input type="checkbox" value="quanxuan" class="quanxuan" id="selectAll"/>全选
      </div>
      <div class="sub_top fl">商品名称</div>
      <div class="sub_top fl">单价</div>
      <div class="sub_top fl">数量</div>
      <div class="sub_top fl">小计</div>
      <div class="sub_top fr">操作</div>
      <div class="clear"></div>
    </div>
    <div id="show_cart">
      <div class="content2 center">
        <div class="sub_content fl ">
          <input type="checkbox" value="quanxuan" class="quanxuan"/>
        </div>
        <div class="sub_content fl"><img src="./image/gwc_xiaomi6.jpg"></div>
        <div class="sub_content fl ft20">小米6全网通6GB内存+64GB 亮黑色</div>
        <div class="sub_content fl ">2499元</div>
        <div class="sub_content fl">
          <input class="shuliang" type="number" value="1" step="1" min="1">
        </div>
        <div class="sub_content fl">2499元</div>
        <div class="sub_content fl"><a href=""></a></div>
        <div class="clear"></div>
      </div>
    </div>
    <div id="pagination">
      <button id="previousPage" value="1">上一页</button>
      <input type="text" value="1" id="txt" style="text-align: center"/>
      <button id="nextPage" value="1">下一页</button>
    </div>
  </div>
  <div class="jiesuandan mt20 center">
    <div class="tishi fl ml20">
      <ul>
        <li><a href="index.html">继续购物</a></li>
        <li>|</li>
        <li>共<span id="totleN">2</span>件商品，已选择<span id="selectN">0</span>件</li>
        <div class="clear"></div>
      </ul>
    </div>
    <div class="jiesuan fr">
      <div class="jiesuanjiage fl">合计（不含运费）：<span id="totalP">0</span></div>
      <div class="jsanniu fr"><input class="jsan" type="button" name="jiesuan" value="去结算"
                                     onclick="SettleAccount()"/></div>
      <div class="clear"></div>
    </div>
    <div class="clear"></div>
  </div>

</div>


<!-- footer -->
<footer class="center">

</footer>

</body>
</html>